import Vue from 'vue'
import Vuex from 'vuex'

import user from './user';

Vue.use(Vuex);//vue的插件机制

//Vuex.Store 构造器选项
const store = new Vuex.Store({
	state:{//存放状态
        strict: true, // 开启严格模式
        cartList: uni.getStorageSync('cartList') || []
	},
    mutations:{
        updateCartList(state,payload){
            state.cartList = payload
            uni.setStorageSync('cartList', payload)
        }
    },
    getters:{
        // 全选
        isAllSelectd(state){
            return state.cartList.every(item => item.goods_select)
        },
        // 选中商品
        selectList(state){
            return state.cartList.filter(item => item.goods_select)
        },
        // 选中商品的数量
        selectListCount(state,getters){
            // forEach 实现
            // let sum = 0;
            // getters.selectList.forEach(item => {
            //     sum += item.goods_count
            // });
            // return sum
            // reduce 实现
            return getters.selectList.reduce((sum,item) => {
              return sum += item.goods_count
            },0)
        },
        //选中商品价格
        selectListPrice(state,getters){
            return getters.selectList.reduce((sum,item) =>{
                return sum += item.goods_count * item.goods_price
            },0)
        }
    },
    modules:{
        user
    }
})
export default store